package com.vecspace.database.model;

import javax.jms.*;

import com.vecspace.utils.log.HLogger;

public class DataChangePublisherImpl implements IDataChangePublisher {
	protected ConnectionFactory connectionFactory;
	protected Connection connection;  
    protected Session session;  
    protected MessageProducer producer;
    
	/**本实例是否有效*/
    protected boolean producerValid = false;
         
	public DataChangePublisherImpl() {
		
	}
	
	
	public ConnectionFactory getConnectionFactory() {
		return connectionFactory;
	}

	public void setConnectionFactory(ConnectionFactory connectionFactory) {
		this.connectionFactory = connectionFactory;
	}

	public void init(){
		
	}
	
	public boolean initMQ(){
		producerValid = false;
		 try {
	        connection = connectionFactory.createConnection();
	        connection.start();
	        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	        
	        producer = session.createProducer(null);
	        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);	
	        producerValid = true;
		  } catch (JMSException jmse) {
			  HLogger.DEFAULT().error(jmse);
			  try {
					if(connection!=null) {
						connection.close();
					}
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	      }
		 return producerValid;
	}
	
	public boolean reconnect(){
		if(producer != null){try {producer.close();} catch (JMSException e) {}}
		if(session != null){try {session.close();} catch (JMSException e) {}}
		if(connection != null){try {connection.close();} catch (JMSException e) {}}
		return initMQ();
	}
	
	@Override
	public boolean valid(){
		if(producerValid) {
			return producerValid;
		}
		synchronized (DataChangePublisherImpl.class) {
			if(producerValid) {
				return producerValid;
			}
			initMQ();
			return producerValid;
		}
	}
	
	@Override
	public void addItem(String tableName, String jsonData){
		if(!valid()) {
			return;
		}
		Message message;
		try {
			message = session.createTextMessage(jsonData);		
		
			Destination destination1 = session.createQueue("DataChange.Add." + tableName);  
	        producer.send(destination1, message);
	        
	        Destination destination2 = session.createTopic("DataChange.Add." + tableName);  
	        producer.send(destination2, message);
	        session.commit();
		} catch (JMSException e) {
			
		}
	}
	
	@Override
	public void deleteItem(String tableName, String jsonData) throws Exception{
		if(!valid()) {
			return;
		}
		
		Message message = session.createTextMessage(jsonData);

		Destination destination1 = session.createQueue("DataChange.Delete." + tableName);  
        producer.send(destination1, message);
        
        Destination destination2 = session.createTopic("DataChange.Delete." + tableName);  
        producer.send(destination2, message);
        session.commit();
	}
	
	@Override
	public void deleteItems(String tableName, String jsonData) throws Exception{
		if(!valid()) {
			return;
		}
		
		Message message = session.createTextMessage(jsonData);

		Destination destination1 = session.createQueue("DataChange.Deletes." + tableName);  
        producer.send(destination1, message);
        
        Destination destination2 = session.createTopic("DataChange.Deletes." + tableName);  
        producer.send(destination2, message);
        session.commit();
	}
	
	@Override
	public void updateItem(String tableName, String jsonData) throws Exception{
		if(!valid()) {
			return;
		}

		Message message = session.createTextMessage(jsonData);

		Destination destination1 = session.createQueue("DataChange.Update." + tableName);  
        producer.send(destination1, message);
        
        Destination destination2 = session.createTopic("DataChange.Update." + tableName);
        producer.send(destination2, message);
        session.commit();
	}
//
//	public String getBrokerURL() {
//		return brokerURL;
//	}
//
//	public void setBrokerURL(String brokerURL) {
//		this.brokerURL = brokerURL;
//	}	
}
